package com.kingwood.algorithm.test200;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;

/**
 * @Author 22025812
 * @Description: TODO
 * @Date: 2024/3/29 16:40
 * @Modified By：
 * @Version 1.0
 */
public class Climb2 {

    public static int getMaxPeekNums(int[] arr, int strength) {
        int len = arr.length;
        boolean noFloor = true;
        for (int i = 0; i < len; i++) {
            if (arr[i] == 0) {
                noFloor = false;
                break;
            }
        }

        if (noFloor) {
            return 0;
        }

        HashSet<Integer> idx = new HashSet<>();
        climb(arr, strength, idx, true);
        reverse(arr);
        climb(arr, strength, idx, false);
        return idx.size();
    }

    public static void climb(int[] arr, int strength, HashSet<Integer> idx, boolean direction) {
        int cost = 0;
        for (int i=1; i < arr.length; i++) {
            // 如果遇到了新的地面，则从新的地面位置开始重新计算消耗的体力
            if (arr[i] == 0) {
                cost = 0;
                continue;
            }

            int diff = arr[i] - arr[i-1];
            if (diff > 0) {
                // 如果是上坡
                cost += diff * 3;
                // 如果是到达了最高点或者到了数组结尾
                if (i + 1 >= arr.length || arr[i] > arr[i+1]) {
                    if (cost <= strength) {
                        if (direction) {
                            idx.add(i);
                        } else {
                            idx.add(arr.length - i - 1);
                        }
                    } else {
                        break;
                    }
                }
            } else if (diff < 0) {
                // 如果是下坡
                cost -= diff * 3;
            }
        }
    }

    public static void reverse(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        while (left < right) {
            int temp = arr[left];
            arr[left] = arr[right];
            arr[right] = temp;
            left++;
            right--;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] arr = Arrays.stream(scanner.nextLine().split(",")).mapToInt(Integer::parseInt).toArray();
        int strength = scanner.nextInt();
        int ans = getMaxPeekNums(arr, strength);
        System.out.println(ans);
    }
}
